/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { WebResponseDataTypeCheck } from '../../../src/checker/performance/WebResponseDataTypeCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/WebResponseDataType', rule, CHECK_MODE.PROJECT2CHECK, WebResponseDataTypeCheck);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('WebResponseDataTypeCheckTest', () => {
    test('WebResponseDataTypeCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        
        // 验证告警内容
        const report = detectFileReports[0];
        expect(report.defect.description).toContain('Avoid using string type data in setResponseData method');
        
        // 打印实际的行列号用于调试
        console.log(`Report 001: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        
        // 验证告警的行列号 - 现在应该指向参数位置
        expect(report.defect.reportLine).toBe(23);
        expect(report.defect.reportColumn).toBe(49);
        expect(report.defect.fixKey).toBe('23%49%64%');
    });

    test('WebResponseDataTypeCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });

    test('WebResponseDataTypeCheckTest_003_ComplexReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeComplexReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 5处应告警：多级变量赋值、函数返回值、对象属性、数组元素、联合类型静态为string
        expect(detectFileReports.length).toBe(5);
        
        // 打印实际的行列号用于调试
        detectFileReports.forEach((report, index) => {
            console.log(`Complex report ${index}: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        });
        
        // 验证告警内容和行列号 - 现在应该指向参数位置
        const expectedLines = [106, 90, 74, 58, 42];
        const expectedColumns = [49, 49, 49, 49, 49];
        const expectedFixKeys = ['106%49%58%', '90%49%59%', '74%49%61%', '58%49%61%', '42%49%58%'];
        
        detectFileReports.forEach((report, index) => {
            expect(report.defect.description).toContain('Avoid using string type data in setResponseData method');
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });

    test('WebResponseDataTypeCheckTest_004_ComplexNoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeComplexNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 5处都不应告警：多级变量赋值为ArrayBuffer、函数返回值、对象属性、数组元素、联合类型静态为ArrayBuffer
        expect(detectFileReports.length).toBe(0);
    });

    test('WebResponseDataTypeCheckTest_005_SuperComplexReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeSuperComplexReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) =>
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 5处应告警
        expect(detectFileReports.length).toBe(5);
        
        // 打印实际的行列号用于调试
        detectFileReports.forEach((report, index) => {
            console.log(`SuperComplex report ${index}: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        });
        
        const expectedLines = [33, 46, 48, 50, 52];
        const expectedColumns = [43, 49, 49, 49, 49];
        const expectedFixKeys = ['33%43%46%', '46%49%59%', '48%49%67%', '50%49%62%', '52%49%58%'];
        
        detectFileReports.forEach((report, index) => {
            expect(report.defect.description).toContain('Avoid using string type data in setResponseData method');
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });

    test('WebResponseDataTypeCheckTest_006_SuperComplexNoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'WebResponseDataTypeSuperComplexNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) =>
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });
})
